package org.stbernardpreserve.learning.effectivejava.staticfactory;

import java.util.ArrayList;
import java.util.List;

public class StaticFactoryExample {
	
	//Bring clarity through naming
	//Not required to create a new object
	//Return an object of a Subtype
	
	private final int size;
	private final String name;
	
	
	protected static List<StaticFactoryExample> staticFactoryExamplelist = new ArrayList<StaticFactoryExample>();
	
	public static StaticFactoryExample giveMeABlueStaticFactoryExample( int size, String name )
	{
		StaticFactoryExample returnStaticFactoryExample = null;
		
		for ( StaticFactoryExample staticFactoryExample : staticFactoryExamplelist)
		{
			if ( staticFactoryExample instanceof StaticFactoryExampleBlue)
			{
				if ( staticFactoryExample.getSize()==size && staticFactoryExample.getName().equalsIgnoreCase(name) ) 
				{
					returnStaticFactoryExample = staticFactoryExample;
				}
			}
		}
		
		if ( returnStaticFactoryExample==null)
		{
			returnStaticFactoryExample = new StaticFactoryExampleBlue(name, size);
			staticFactoryExamplelist.add(returnStaticFactoryExample);
			
		}
		
		return returnStaticFactoryExample;
		
	}
	
	public static StaticFactoryExample giveMeARedStaticFactoryExample( int size, String name )
	{
		StaticFactoryExample returnStaticFactoryExample = null;
		
		for ( StaticFactoryExample staticFactoryExample : staticFactoryExamplelist)
		{
			if ( staticFactoryExample instanceof StaticFactoryExampleRed)
			{
				if ( staticFactoryExample.getSize()==size && staticFactoryExample.getName().equalsIgnoreCase(name) ) 
				{
					returnStaticFactoryExample = staticFactoryExample;
				}
			}
		}
		
		if ( returnStaticFactoryExample==null)
		{
			returnStaticFactoryExample = new StaticFactoryExampleRed(name,size );
			staticFactoryExamplelist.add(returnStaticFactoryExample);
			
		}
		
		return returnStaticFactoryExample;
		
	}

	
	protected StaticFactoryExample(String name, int size)
	{
		this.name=name;
		this.size=size;
		
	}
	
	public int getSize()
	{
		return size;
	}
	
	public String getName()
	{
		return name;
	}
	
	
	
	
	
	

}
